﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _2011DBProject
{
    public partial class GameEnd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnGo.Attributes.Add("onclick", "return confirm('기록을 입력하시겠습니까?');");
            btnLevelGo.Attributes.Add("onclick", "return confirm('입력하신 스탯이 정확합니까?');");
            lblNG.Text = "3000";

            Label tmplbl = (Label)(DataList1.Controls[0].Controls[2].FindControl("LVLLabel"));
            decimal tmpNo = 300 - Convert.ToDecimal(tmplbl.Text)*10;
            lblExp.Text = tmpNo.ToString();
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {
            //기록경신
            DataSet1TableAdapters.CHARACTER1TableAdapter taCharacter = new DataSet1TableAdapters.CHARACTER1TableAdapter();
            taCharacter.Connection.ConnectionString = Settings.strConnectionString;
            DataSet1.CHARACTER1DataTable dtCharacter = taCharacter.GetDataAllCol(Settings.decServer, Settings.strCharacterName);
            DataSet1.CHARACTER1Row row = dtCharacter[0];

            Label8.Text = row.AVG_PUTS.ToString();
            Label9.Text = row.COUNT_PUTS.ToString();

            row.AVG_PUTS = row.AVG_PUTS * row.COUNT_PUTS + (decimal)float.Parse(txtAvgPut.Text) * (decimal)float.Parse(txtCountPut.Text) / (row.COUNT_PUTS + (decimal)float.Parse(txtCountPut.Text));
            row.AVG_SHOTS = row.AVG_SHOTS * row.COUNT_SHOTS + (decimal)float.Parse(txtAvgShot.Text) * (decimal)float.Parse(txtCountShot.Text) / (row.COUNT_SHOTS + (decimal)float.Parse(txtCountShot.Text));

            row.COUNT_PUTS += (decimal)float.Parse(txtCountPut.Text);       //평균타수
            row.COUNT_SHOTS += (decimal)float.Parse(txtCountShot.Text);

            if(row.MAX_DISTANCE < (decimal)float.Parse(txtMaxDis.Text))     //최대비거리
                row.MAX_DISTANCE = (decimal)float.Parse(txtMaxDis.Text);

            row.HOLE_IN_ONE_QTY += (decimal)float.Parse(txtHole.Text);      //홀인원

            row.TIREDNESS += 25;

            Label tmplbl2 = (Label)DataList2.Controls[0].Controls[2].FindControl("REWARD_RATELabel");

            row.NG_AMOUNT += 3000 * (decimal)float.Parse(tmplbl2.Text) / 100;                 //일단그냥 3000
            
            tmplbl2 = (Label)DataList2.Controls[0].Controls[1].FindControl("EXP_RATELabel");

            row.EXPERIENCE += (300 - row.LVL * 10) * (decimal)float.Parse(tmplbl2.Text) / 100; //나중에 증가경험치,난이도 추가해

            taCharacter.Update(dtCharacter);

            if ((row.LVL * row.LVL * 200) < row.EXPERIENCE)                 //레벨업이라면.
                levelUp(row.LVL, row.EXPERIENCE - row.LVL * row.LVL * 200);
            else
                Response.Redirect("Gameroom.aspx");


        }

        private void levelUp(decimal level, decimal remind)
        {
            DataSet1TableAdapters.CHARACTER1TableAdapter taCharacter = new DataSet1TableAdapters.CHARACTER1TableAdapter();
            taCharacter.Connection.ConnectionString = Settings.strConnectionString;
            DataSet1.CHARACTER1DataTable dtCharacter = taCharacter.GetDataAllCol(Settings.decServer, Settings.strCharacterName);
            DataSet1.CHARACTER1Row row = dtCharacter[0];

            row.LVL = level+1;
            row.EXP_TO_NEXT_LEVEL = remind / (row.LVL * row.LVL * 200 - level * level * 200);


            taCharacter.Update(dtCharacter);

            btnGo.Visible = false;
            lblLevelUpMsg.Visible = true;
            Label4.Visible = true;
            Label5.Visible = true;
            Label6.Visible = true;
            Label7.Visible = true;
            btnLevelGo.Visible = true;
            txtImp.Visible = true;
            txtPower.Visible = true;
            txtStr.Visible = true;
            txtTech.Visible = true;
        }

        protected void btnLevelGo_Click(object sender, EventArgs e)
        {
            if (txtImp.Text == null)
                txtImp.Text = "0";
            if (txtPower.Text == null)
                txtPower.Text = "0";
            if (txtStr.Text == null)
                txtStr.Text = "0";
            if (txtTech.Text == null)
                txtTech.Text = "0";

            if (Convert.ToDecimal(txtImp.Text) + Convert.ToDecimal(txtPower.Text) + Convert.ToDecimal(txtStr.Text) + Convert.ToDecimal(txtTech.Text) == 2)
            {
                DataSet1TableAdapters.CHARACTER1TableAdapter taCharacter = new DataSet1TableAdapters.CHARACTER1TableAdapter();
                taCharacter.Connection.ConnectionString = Settings.strConnectionString;
                DataSet1.CHARACTER1DataTable dtCharacter = taCharacter.GetDataAllCol(Settings.decServer, Settings.strCharacterName);
                DataSet1.CHARACTER1Row row = dtCharacter[0];

                row.IMPACT += Convert.ToDecimal(txtImp.Text);
                row.POWER += Convert.ToDecimal(txtPower.Text);
                row.STRENGTH += Convert.ToDecimal(txtStr.Text);
                row.TECHNIQUE += Convert.ToDecimal(txtTech.Text);

                taCharacter.Update(dtCharacter);

                Response.Redirect("Gameroom.aspx");
            }
            else
            {
                string script = "<script>alert('2만큼의 스탯만 입력해주세요!');</script>";
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "confirm", script);
            }
        }

    }
}